﻿
using System;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using DataAccess;

namespace Business
{
    public class AddressBusiness
    {

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static AddressBusiness Instance { get; private set; }

        /// <summary>
        /// Initializes the <see cref="AddressBusiness" /> class.
        /// </summary>
        static AddressBusiness() { Instance = new AddressBusiness(); }

        /// <summary>
        /// Searches the Address.
        /// </summary>
        /// <param name="street">The street.</param>
        /// <param name="postcode">The post code.</param>
        /// <param name="town">The town.</param>
        /// <returns></returns>
        public Collection<Entities.Address> SearchAddress(string street, string postcode, string town)
        {
            if (street == null || postcode == null || town == null)
            {

                return null;
            }
            try
            {
                return AddressDataAccess.Instance.ListAddress(street, postcode, town);
            }
            catch (SqlException)
            {

                return null;
            }

        }
        /// <summary>
        /// Pagings the Address
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="lstAddress">The LST address.</param>
        /// <returns></returns>
        public Collection<Entities.Address> PagingAddress(int pageNumber, int pageSize, Collection<Entities.Address> lstAddress)
        {

            if (lstAddress != null)
            {
                if (pageSize > 0)
                {
                    var totalpage = lstAddress.Count % pageSize == 0 ? lstAddress.Count / pageSize : lstAddress.Count / pageSize + 1;
                    if (pageNumber <= totalpage && pageNumber > 0)

                        return new Collection<Entities.Address>(lstAddress.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());

                }
                return null;
            }
            return null;
        }
        /// <summary>
        /// Gets the address.
        /// </summary>
        /// <param name="addressId">The address id.</param>
        /// <returns></returns>
        public Entities.Address GetAddress(string addressId)
        {
            try
            {
                if (!string.IsNullOrEmpty(addressId) && addressId != Guid.Empty.ToString())
                    return AddressDataAccess.Instance.GetAddress(addressId);
                return null;
            }
            catch (SqlException exception)
            {


                return null;
            }

        }
        /// <summary>
        /// Inserts the specified address.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public string Insert(Entities.Address address)
        {
            try
            {
                return AddressDataAccess.Instance.Insert(address)>0?"Successfull":"Insert fail";
            }
            catch (SqlException exception)
            {
                return exception.Message;
            }
        }
    }

}
